import axios from "axios";

const instance = axios.create({
  baseURL: "http://localhost:8080", // 这里填写你的API基础URL
  timeout: 10000,
});

// 请求拦截器
instance.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem("token");
    if (token) {
      config.headers.token = token;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
instance.interceptors.response.use(
  (response) => {
    // 如果是 blob，直接返回 response，让调用方自己处理
    if (response.data instanceof Blob) {
      return response;
    }
    console.log(response);
    if (response.data.code == 401) {
      localStorage.removeItem("token");
      window.location.href = "/login";
      return Promise.reject("未登录");
    }
    return response.data;
  },
  (error) => {
    console.error("请求错误：", error);
    return Promise.reject(error);
  }
);

// 头像相关API
export const avatarApi = {
  // 上传头像
  upload: (formData: FormData) =>
    instance.post("/avatar/upload", formData, {
      headers: { "Content-Type": "multipart/form-data" },
    }),
};

// 教师相关API
export const teacherApi = {
  // 分页查询教师
  selectPage: (params: any) => instance.get("/teacher/selectPage", { params }),

  // 查询所有教师
  selectAll: (params?: any) => instance.get("/teacher/selectAll", { params }),

  // 添加教师
  add: (data: any) => instance.post("/teacher/add", data),

  // 更新教师
  update: (data: any) => instance.put("/teacher/update", data),

  // 删除教师
  delete: (id: number) => instance.delete(`/teacher/delete/${id}`),

  // 批量删除教师
  deleteBatch: (data: any[]) =>
    instance.delete("/teacher/deleteBatch", { data }),

  // 批量导入教师数据
  importData: (formData: FormData) =>
    instance.post("/teacher/import", formData, {
      headers: { "Content-Type": "multipart/form-data" },
    }),

  // 导出教师数据
  exportData: (params?: any) =>
    instance.get("/teacher/export", {
      params,
      responseType: "blob",
    }),
};

// 学生相关API
export const studentApi = {
  // 分页查询学生
  selectPage: (params: any) => instance.get("/student/selectPage", { params }),

  // 查询所有学生
  selectAll: (params?: any) => instance.get("/student/selectAll", { params }),

  // 添加学生
  add: (data: any) => instance.post("/student/add", data),

  // 更新学生
  update: (data: any) => instance.put("/student/update", data),

  // 删除学生
  delete: (id: number) => instance.delete(`/student/delete/${id}`),

  // 批量删除学生
  deleteBatch: (ids: string) =>
    instance.delete("/student/deleteBatch", {
      data: { ids },
    }),

  // 批量导入学生数据
  importData: (formData: FormData) =>
    instance.post("/student/import", formData, {
      headers: { "Content-Type": "multipart/form-data" },
    }),

  // 导出学生数据
  exportData: (params?: any) =>
    instance.get("/student/export", {
      params,
      responseType: "blob",
    }),
};

// 管理员相关API
export const adminApi = {
  // 分页查询管理员
  selectPage: (params: any) => instance.get("/admin/selectPage", { params }),

  // 查询所有管理员
  selectAll: (params?: any) => instance.get("/admin/selectAll", { params }),

  // 添加管理员
  add: (data: any) => instance.post("/admin/add", data),

  // 更新管理员
  update: (data: any) => instance.put("/admin/update", data),

  // 删除管理员
  delete: (id: number) => instance.delete(`/admin/delete/${id}`),

  // 批量删除管理员
  deleteBatch: (data: any[]) => instance.delete("/admin/deleteBatch", { data }),

  // 批量导入管理员数据
  importData: (formData: FormData) =>
    instance.post("/admin/import", formData, {
      headers: { "Content-Type": "multipart/form-data" },
    }),

  // 导出管理员数据
  exportData: (params?: any) =>
    instance.get("/admin/export", {
      params,
      responseType: "blob",
    }),
};

// 课程相关API
export const subjectApi = {
  // 分页查询课程
  selectPage: (params: any) => instance.get("/subject/page", { params }),

  // 查询所有课程
  selectAll: (params?: any) => instance.get("/subject", { params }),

  // 添加课程
  add: (data: any) => instance.post("/subject", data),

  // 更新课程
  update: (data: any) => instance.put("/subject", data),

  // 删除课程
  delete: (id: number) => instance.delete(`/subject/${id}`),

  // 批量删除课程
  deleteBatch: (data: any[]) => instance.delete("/subject/batch", { data }),
};

// 任务相关API
export const taskApi = {
  // 分页查询任务
  selectPage: (params: any) => instance.get("/task/page", { params }),

  // 查询所有任务
  selectAll: (params?: any) => instance.get("/task", { params }),

  // 添加任务
  add: (data: any) => instance.post("/task", data),

  // 更新任务
  update: (data: any) => instance.put("/task", data),

  // 删除任务
  delete: (id: number) => instance.delete(`/task/${id}`),

  // 批量删除任务
  deleteBatch: (data: any[]) => instance.delete("/task/batch", { data }),

  // 根据课程ID查询任务
  selectBySubjectId: (subjectId: number) =>
    instance.get(`/task/subject/${subjectId}`),
};

// 学生课程关联相关API
export const subjectStudentApi = {
  // 分页查询课程学生关联记录
  selectPage: (params: any) =>
    instance.get("/subjectStudent/selectPage", { params }),

  // 根据课程ID查询学生
  getBySubjectId: (subjectId: number) =>
    instance.get("/subjectStudent/bySubjectId", {
      params: { subjectId },
    }),

  // 根据学生ID查询课程
  getByStudentId: (studentId: number) =>
    instance.get("/subjectStudent/byStudentId", {
      params: { studentId },
    }),

  // 添加单个学生到课程
  add: (data: any) => instance.post("/subjectStudent/add", data),

  // 批量添加学生到课程
  batchAdd: (subjectId: number, studentIds: number[]) =>
    instance.post("/subjectStudent/batchAdd", studentIds, {
      params: { subjectId },
    }),

  // 从课程中移除单个学生
  remove: (data: any) => instance.delete("/subjectStudent/remove", { data }),

  // 批量从课程中移除学生
  batchRemove: (subjectId: number, studentIds: number[]) =>
    instance.delete("/subjectStudent/batchRemove", {
      data: studentIds,
      params: { subjectId },
    }),
};

// 用户认证相关API
export const authApi = {
  // 用户登录
  login: (data: any) => instance.post("/login", data),

  // 用户注册
  register: (data: any) => instance.post("/register", data),

  userInfo: () => instance.get("/userInfo"),
};